package com.tang.transmission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tang.common.entity.companyJob.Job;
import com.tang.common.entity.resumeTemplate.ResumeTemplate;
import com.tang.common.exception.RecruitExceptionEnum;
import com.tang.common.uniform.Result;
import com.tang.common.utils.JwtUtil;
import com.tang.common.utils.UpLoad;
import com.tang.transmission.service.ResumeTemplateService;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 唐朝阳
 * @since 2022-05-18
 */
@RestController
@RequestMapping("/resumeTemplate")
public class ResumeTemplateController {
    @Autowired
    private ResumeTemplateService resumeTemplateService;

    @ApiOperation("简历模板列表")
    @RequestMapping("/resumeTemplateList")
    public Result getRTList(String keywords, Integer currentPage, Integer pageSize,Integer filter) {
        PageInfo<ResumeTemplate> list = null;
        if(filter==null){
         list = resumeTemplateService.listResume(keywords, currentPage, pageSize);
        }else {
            list =  resumeTemplateService.listFilterResume(currentPage, pageSize,filter);
        }

        return Result.success(list);
    }


    @RequestMapping("/getRT")
    public Result getRT(Integer resumeTemplateId) {
        ResumeTemplate rt = resumeTemplateService.getById(resumeTemplateId);
        if(rt!=null){
            return  Result.success(rt);
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }

    }

    @ApiOperation("上架下架简历模板")
    @PutMapping("/shelveResumeTemplate")
    public Result shelveJob(int resumeTemplateId) {
        ResumeTemplate rt = resumeTemplateService.getById(resumeTemplateId);
        if (rt != null && rt.getDeleteFlag() != 0) {
            rt.setState(rt.getState() == 1 ? 0 : 1);
//            rt.setUpdateTime(new Date());
            boolean b = resumeTemplateService.updateById(rt);
            if (b) {
                return Result.success("删除成功");
            } else {
                return Result.error(RecruitExceptionEnum.DELETE_FAILED);
            }
        }else{
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
}

    @ApiOperation("删除简历模板")
    @RequestMapping("/delResumeTemplate")
    public Result delCompany(Integer resumeTemplateId) {
        ResumeTemplate rt = resumeTemplateService.getById(resumeTemplateId);
        if (rt != null) {
            rt.setDeleteFlag(0);
            boolean b = resumeTemplateService.updateById(rt);
            if (b) {
                return Result.success("删除成功");
            } else {
                return Result.error(RecruitExceptionEnum.DELETE_FAILED);
            }
        } else {
            return Result.error(RecruitExceptionEnum.NO_DATA);
        }
    }
    @ApiOperation("添加简历模板")
    @RequestMapping("/upload")
    public Result delCompany(@RequestParam("name") String name,
                             @RequestParam("pay") Integer pay,
                             @RequestParam("file") List<MultipartFile> files,
                             @RequestParam("img") List<MultipartFile> imgs,
                             HttpServletRequest request
                             ) {
        List<String> file = UpLoad.upload(files);
        List<String> img = UpLoad.upload(imgs);
        ResumeTemplate rt;
        if(!file.isEmpty() && !img.isEmpty()&&!name.isEmpty()){
        String token = request.getHeader("TOKEN");
        String adminName = JwtUtil.get(token, "username");
        rt= new ResumeTemplate();
        rt.setName(name);
        rt.setPay(pay);
        rt.setAdminName(adminName);
            rt.setCoverUrl(img.get(0));
            rt.setDownloadUrl(file.get(0));
        }else{
            return Result.error(RecruitExceptionEnum.LESS_PARAMETER);
        }
        boolean b = resumeTemplateService.save(rt);
        if (b) {
            return Result.success("上传成功");
        }
        return Result.error(RecruitExceptionEnum.ADD_FAILED);
    }
    @ApiOperation("编辑简历模板")
    @RequestMapping("/editResumeTemplate")
    public Result editResumeTemplate(Integer resumeTemplateId,String name,Integer pay) {
        ResumeTemplate rt = resumeTemplateService.getById(resumeTemplateId);
        if (rt != null) {
            rt.setName(name);
            rt.setPay(pay);
            boolean b = resumeTemplateService.updateById(rt);
            if (b) {
                return Result.success("编辑成功");
            }
        }
        return Result.error(RecruitExceptionEnum.UPDATE_FAILED);
    }

    @RequestMapping("/resumeTemplateCount")
    public Integer getResumeTemplateCount(){
        return resumeTemplateService.count(null);
    }
}
